package mn.idax.exchange.otc.config;

import mn.idax.exchange.auth.interceptor.CheckTokenInterceptor;
import mn.idax.exchange.auth.interceptor.SecurityInterceptor;
import mn.idax.exchange.auth.interfaces.AuthService;
import mn.idax.exchange.otc.constant.RequestUrlConstant;
import mn.idax.exchange.otc.interceptor.LoginAuthInterceptor;
import mn.idax.exchange.otc.interceptor.OtcSecurityInterceptor;
import mn.idax.exchange.otc.service.OTCUserService;
import mn.idax.exchange.otc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private AuthService authService;
    @Autowired
    private OTCUserService otcUserService;
    @Autowired
    private UserService userService;

    public void addInterceptors(InterceptorRegistry registry) {
        // 用户登录鉴权校验拦截器
        /**
         * TODO: 2018/7/27 后续考虑通过注解形式来判断是否需鉴权
         * 说明:一：启动
         *      (1).需先将本地的zookeeper以及redis开启
         *      (2).启动auth项目的auth-api子模块
         *     二：禁用
         *      (1).关闭拦截器以及注释上层的AuthService
         *      (2).关闭dubbo-provider.xml中对外暴露接口
         */

        OtcSecurityInterceptor securityInterceptoropen = new OtcSecurityInterceptor();
        registry.addInterceptor(securityInterceptoropen).addPathPatterns(RequestUrlConstant.MODULE_OPEN + RequestUrlConstant.OPEN_PRIVATE + "/**");

        LoginAuthInterceptor interceptor = new LoginAuthInterceptor();
        interceptor.setAuthService(authService);
        interceptor.setOtcUserService(otcUserService);
        interceptor.setUserService(userService);
        registry.addInterceptor(interceptor).addPathPatterns("/api/**")
                .excludePathPatterns(RequestUrlConstant.MODULE_AD+RequestUrlConstant.AD_RECOMMENDADS)
                .excludePathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.AD_PAY_METHODS)
                .excludePathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.AD_PAY_UNIT)
                .excludePathPatterns(RequestUrlConstant.PAYMODEDETAIL+RequestUrlConstant.GET_PAYMODE)
                .excludePathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.PAY_TYPE)
                .excludePathPatterns(RequestUrlConstant.MODULE_USER+RequestUrlConstant.GET_SYSTEM)
                .excludePathPatterns(RequestUrlConstant.MODULE_OPEN + RequestUrlConstant.OPEN_PRIVATE + "/**")
                .excludePathPatterns(RequestUrlConstant.MODULE_OPEN + RequestUrlConstant.OPEN_PUBLIC + "/**")
                .excludePathPatterns("/api/v2/ad"+RequestUrlConstant.AD_LIST);

        //可走不走token验证
        CheckTokenInterceptor simpleCheck=new CheckTokenInterceptor();
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(simpleCheck);
        simpleCheck.setAuthService(authService);
        simpleCheck.setSimpleCheck(true);
        interceptorRegistration.addPathPatterns("/api/v2/ad/" + RequestUrlConstant.AD_LIST);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_AD+RequestUrlConstant.AD_RECOMMENDADS);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.AD_PAY_METHODS);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.AD_PAY_UNIT);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.PAYMODEDETAIL+RequestUrlConstant.GET_PAYMODE);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.PAY_TYPE);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_AD+RequestUrlConstant.AD_INFO);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_AD+RequestUrlConstant.AD_SUMMARY);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_AD+RequestUrlConstant.AD_GET_MAX_PRICE);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.SYS_GET_COIN_CODE);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.SYS_GET_EXPIRE_DAY_NUM);
        interceptorRegistration.addPathPatterns(RequestUrlConstant.MODULE_SYS+RequestUrlConstant.SYS_GET_COMPLAINT_TYPE);
        interceptorRegistration.addPathPatterns("/api/v2/ad"+RequestUrlConstant.AD_GET_MIN_PRICE);

        /**限流拦截器**/
        SecurityInterceptor securityInterceptor = new SecurityInterceptor();
        registry.addInterceptor(securityInterceptor)
                .addPathPatterns("/api/v1/**")
                .addPathPatterns("/api/v2/**")
                .excludePathPatterns("/api/v1/user/getMyExamineList");
        securityInterceptor.setAuthService(authService);
    }

}